package ed.hbut.supermarket1.service.impl;

import ed.hbut.supermarket1.dto.FinanceDto;
import ed.hbut.supermarket1.entity.Cost;
import ed.hbut.supermarket1.mapper.FinanceMapper;
import ed.hbut.supermarket1.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    public FinanceMapper financeMapper;


    @Override
    public List<FinanceDto> getSalePrice(Integer year, Integer month) {

        List<FinanceDto> list ;
        List<FinanceDto> list1 = new ArrayList<>(12);
        for (int i = 1; i <= 12; i++) {
            list1.add(new FinanceDto(i, null)); // 初始化填充0
        }



        if (month == null) {

             list =financeMapper.getSalePriceBymonth(year);
        }
        else {
            list =financeMapper.getSalePriceByDay(year,month);
        }
        for (FinanceDto data : list) {
            int monthIndex = data.getIndex();
            // 确保月份有效（1-12）
            if (monthIndex >= 1 && monthIndex <= 12) {
                list1.set(monthIndex - 1, data); // 替换对应位置
            }
        }
        return list1;
    }

    @Override
    public List<FinanceDto> getCost(Integer year, Integer month) {
        List<FinanceDto> list ;
        List<FinanceDto> list1 = new ArrayList<>(12);
        for (int i = 1; i <= 12; i++) {
            list1.add(new FinanceDto(i, null)); // 初始化填充0
        }

            list =financeMapper.getCostPriceBymonth(year);
        for (FinanceDto data : list) {
            int monthIndex = data.getIndex();
            // 确保月份有效（1-12）
            if (monthIndex >= 1 && monthIndex <= 12) {
                list1.set(monthIndex - 1, data); // 替换对应位置
            }
        }


        return list1;
    }

    @Override
    public List<Cost> getCostList() {

        return financeMapper.getCostList();
    }
}
